import { createRouter, createWebHistory } from 'vue-router'
import homeView from "../views/homeViews/index.vue"
import layout from "../views/layout.vue"
import login from "../views/loginInfo/index.vue"
import {userLoginStore} from '../stores/loginStore'
import { userMenuStore } from '../stores/menuStore'


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'layout',
      component: layout,
      /*
        meta:{
            requiresAuth:true
          }
        表示必须登录后才能访问
      */
      meta:{
        requiresAuth:true
      },
      children: [
        {
          path: '/',
          name: 'home',
          component: homeView,
          meta:{
            requiresAuth:true,
            key:'首页'
          }
        },
        {
          path:"/projectInfo",
          name:"projectInfo",
          component:()=>import("../views/projectInfo/index.vue"),
          meta:{
            requiresAuth:true,
            key:'项目基础信息'
          }
        },
        {
          path:"/tunnellInfo",
          name:"tunnellInfo",
          component:()=>import("../views/tunnellInfo/index.vue"),
          meta:{
            requiresAuth:true,
            key:'隧道设计信息'
          }
        },
        // {
        //   path:"/workManage",
        //   name:"workManage",
        //   component:()=>import("../views/workManage/index.vue"),
        //   meta:{
        //     requiresAuth:true,
        //     key:'工作监督管理'
        //   }
        // },
        {
          path:"/buildManage",
          name:"buildManage",
          component:()=>import("../views/buildManage/index.vue"),
          meta:{
            requiresAuth:true,
            key:'施工监控检测'
          }
        },
        {
          path:"/geologicslInfo",
          name:"geologicslInfo",
          component:()=>import("../views/geologicslInfo/index.vue"),
          meta:{
            requiresAuth:true,
            key:'超前地质预报'
          }
        },
        {
          path:"/systemManage",
          name:"systemManage",
          component:()=>import("../views/systemManage/index.vue"),
          meta:{
            requiresAuth:true,
            key:'系统信息管理'
          }
        },
        {
          path:"/userCenter",
          name:"userCenter",
          component:()=>import("../views/userCenter/index.vue"),
          meta:{
            requiresAuth:true,
            key:'个人中心'
          }
        },
        {
          path:"/plan",
          name:"plan",
          component:()=>import("../views/buildPlan/index.vue"),
          meta:{
            requiresAuth:true,
            key:'检测计划'
          }
        },
        {
          path:"/section",
          name:"section",
          component:()=>import("../views/buildSection/index.vue"),
          meta:{
            requiresAuth:true,
            key:'切面检测'
          }
        }
      ]
    },
    {
      path:"/login",
      name:"login",
      component:login
    },
    {
      path:"/pdf/:id",
      name:"pdf",
      component:() => import("../views/tunnellInfo/PDFViewer/index.vue")
    },
    {
      path:"/:pathMatch(.*)*",
      name:"notFound",
      component:()=>import('../views/notFound/index.vue')
    }
  ]
})

  /*
  路由权限
  */
 router.beforeEach((to,from,next)=>{
  if(to.meta.requiresAuth){
    // 验证登录状态
    const loginStore=userLoginStore();
    if(!loginStore.token){
      next({
        path:"/login"
      })
    }else{
      next();
    }
  }else{
    next();
  }
 })

 router.afterEach((to,from)=>{
  // 存储路由路径
  localStorage.setItem('active',to.path)
  if(to.meta.key){
    // 存储信息
    const menuStore =userMenuStore();
    menuStore.breadCrumb=to.meta.key
  }
 })

export default router
